<template>
    <a-config-provider :locale="locale">
        <div id="app">
            <RouterView/>
        </div>
    </a-config-provider>
</template>
<script>
import {RouterView} from 'vue-router'
import useMain from "@/stores/useMain.js";
import EventListener from "./views/layout/js/EventListener";

/*引入monaco相关语言及提示*/
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker'
import cssWorker from 'monaco-editor/esm/vs/language/css/css.worker?worker'
import htmlWorker from 'monaco-editor/esm/vs/language/html/html.worker?worker'
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker'
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';

import zhCN from 'ant-design-vue/es/locale/zh_CN';

window.monacoWorkConfig = {
    jsonWorker,
    cssWorker,
    htmlWorker,
    tsWorker,
    EditorWorker
}

export default {
    provide() {
        return {
            /**
             * 组件模块映射，其他地方可以通过inject 注入使用
             */
            appModules: this.modules,
            /**
             * 广播频道，在应用中可以订阅广播.
             */
            broadCastChannel: this.broadChannel
        }
    },
    mixins: [EventListener],
    data() {
        return {
            locale: zhCN,
            modules: import.meta.glob("@/views/**/*.vue"),
            broadChannel: new BroadcastChannel('broadCastChannel')
        }
    },
    created() {
        window['jpaas-base-url'] = import.meta.env.JPAAS_BASE_URL;
        let appKey = this.getAppKey();
        let storeKey = "appStore_" + appKey;
        if (sessionStorage.getItem(storeKey)) {
            let state = JSON.parse(sessionStorage.getItem(storeKey));
            let _state = Object.assign({}, useMain().$state, state);
            useMain().$patch(_state);
        }
        //变化时 将需要的属性存入sessionStorage
        useMain().$subscribe((args, state) => {
            sessionStorage.setItem(storeKey, JSON.stringify(useMain().getSessionStorage()));
        })
        this.initLinkLog();
    },

    methods: {
        getAppKey() {
            let path = window.location.pathname;
            let baseUrl = import.meta.env.JPAAS_BASE_URL;
            let paths = path.split("/");
            let _idx = paths.findIndex(item => item && baseUrl.includes(item))
            let appKey = paths[_idx + 1] ? paths[_idx + 1] : "";
            return appKey;
        },
        getFilePath(fileId) {
            return "/api/api-system/system/core/sysFile/public/download/" + fileId;
        },
        initLinkLog() {
            let fileId = localStorage.getItem("linkLogoUrl");
            if (fileId) {
                const oldLink = document.head.querySelector("[rel='icon']");
                if (oldLink) {
                    oldLink.href = this.getFilePath(fileId);
                } else {
                    const link = document.createElement('link');
                    link.rel = 'icon';
                    link.href = this.getFilePath(fileId);
                    document.head.appendChild(link);
                }
            }
        }
    },

}
</script>
<style lang="less">
#app {
    height: 100%;
    width: 100%;
}
</style>
